import "package:ct_pa_majordomo/config/theme_vars.dart";
import "package:ct_pa_majordomo/pages/apartment/api.dart";
import "package:flutter/material.dart";

typedef ModalBottomSheetBuilder = Widget Function(BuildContext context);

void showSelectBuilding({
  required BuildContext context,
  required int buildingId,
  required double statusBarHeight,
  List? list,
  void Function(int, String)? onChange,
}) async {
  showModalBottomSheet(
    context: context,
    isScrollControlled: true,
    enableDrag: false, // 是否可以往下滑动关闭
    isDismissible: false, // 点击遮罩是否关闭
    builder: (BuildContext context) {
      list = list ?? [];
      int character = buildingId;
      return StatefulBuilder(
          builder: (context, setSheetState) {
            return Container(
              color: ThemeVars.colorPrimaryDark,
              padding: EdgeInsets.only(top: statusBarHeight),
              child: Scaffold(
                appBar: AppBar(
                  iconTheme: const IconThemeData(color: Colors.white),
                  // titleTextStyle: const TextStyle(color: Colors.white),
                  backgroundColor: ThemeVars.colorPrimaryDark,
                  leading: IconButton(
                    onPressed: () {
                      Navigator.pop(context);
                    },
                    icon: const Icon(Icons.arrow_back_ios_new),
                  ),
                  title: const Text("请选择公寓", style: TextStyle(color: Colors.white),),
                  centerTitle: true,
                ),
                body: ListView.separated(
                  padding: const EdgeInsets.all(ThemeVars.paddingPage),
                  itemBuilder: (context, index) {
                    return GestureDetector(
                      behavior: HitTestBehavior.opaque,
                      onTap: () {
                        setSheetState(() {
                          character = list![index]["buildingId"];
                          String name = list![index]["name"];
                          if (onChange != null) onChange(character, name);
                          Navigator.of(context).pop();
                        });
                      },
                      child: DecoratedBox(
                          decoration: BoxDecoration(
                            borderRadius: BorderRadius.circular(6),
                            boxShadow: const [ThemeVars.boxShadow],
                            color: Colors.white,
                          ),
                          child: Container(
                            padding: const EdgeInsets.all(16),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Text(list![index]["name"], style: const TextStyle(fontSize: 18),),
                                character == list![index]["buildingId"]
                                    ? const Icon(Icons.check_circle, color: ThemeVars.colorPrimaryDark,)
                                    : const Icon(Icons.circle_outlined, color: ThemeVars.colorPrimaryDark,),
                              ],
                            ),
                          )
                      ),
                    );
                  },
                  separatorBuilder: (context, index) => const SizedBox(height: 12),
                  itemCount: list!.length,
                ),
              ),
            );
          }
      );
    },
  );
}
